/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.stdlib.distance.type;

import axil.api.AxilObject;
import axil.api.AxilType;
import axil.definition.UnitDefinition;
import axil.engine.Repository;
import axil.framework.type.AbstractUnitizedValue;
import axil.framework.type.Mathematical;
import axil.framework.type.Sized;
import axil.stdlib.numbers.type.Int;
import axil.stdlib.numbers.type.Num;

import java.math.RoundingMode;


/**
 * An Axil unitized type for distances, supporting extended literals.
 */
public class Distance extends AbstractUnitizedValue<Distance>
	implements Sized, Mathematical {
	public static final AxilType type = Distance_Type.object;
	private static Distance zero = null;

	private Num value;
	private UnitDefinition units;


	public Distance(Num value, UnitDefinition units) {
		this.value = value;
		this.units = units;
	}


	/**
	 * Get the type metadata for this object. The type metadata contains
	 * information about the type itself, as an object.
	 */
	public AxilType type() {
		return type;
	}


	/**
	 * Get the units for this unitized value. The units returned are never null.
	 */
	public UnitDefinition units() {
		return units;
	}


	/**
	 * Get the magnitude only of this object. This is the numeric value
	 * excluding any units.
	 */
	public Num magnitude() {
		return value;
	}


	/**
	 * Return the canonical representation of a zero value in this numeric
	 * type.
	 */
	public Num zero() {
		if (zero == null) {
			zero = new Distance(Int.zero, Repository.instance().unit("meter"));
		}
		return zero;
	}


	/**
	 * Add the given value to this value, returning a new object. The given
	 * value is never null or nil. The value may be a numeric type of any other
	 * type of object that can sensibly implement the Added interface.
	 */
	public AxilObject add(AxilObject object) {
		if (object instanceof Distance) {
			Distance other = equalize(object);
			return new Distance((Num)value.add(other.value), units);
		}
		return new Distance((Num)value.add(object), units);
	}


	/**
	 * Subtract the given object from this object. The given value is never
	 * null or nil. The value may be a numeric type of any other type of object
	 * that can sensibly implement the Subtracted interface.
	 */
	public AxilObject subtract(AxilObject n) {
		if (n instanceof Distance) {
			Distance other = equalize(n);
			return new Distance((Num)value.subtract(other.value), units);
		}
		return new Distance((Num)value.subtract(n), units);
	}


	/**
	 * Multiply the given number by this number. This method can safely assume
	 * that the given value is not null or nil, but can be any of the numeric
	 * types.
	 */
	public Num multiply(Num n) {
		if (n instanceof Distance) {
			Distance other = equalize(n);
			return new Distance(value.multiply(other.value), units);
		}
		return new Distance(value.multiply(n), units);
	}


	/**
	 * Divide this number by the given number. This method can safely assume
	 * that the given value is not null or nil, but can be any of the numeric
	 * types.
	 */
	public Num divide(Num n) {
		if (n instanceof Distance) {
			Distance other = equalize(n);
			return new Distance(value.divide(other.value), units);
		}
		return new Distance(value.divide(n), units);
	}


	/**
	 * Round this value according to the rules of the given rounding mode and
	 * to the given number of significant fractional digits. A value of zero
	 * rounds this to a whole number.
	 */
	public Num round(RoundingMode mode, int digits) {
		Num v = value.round(mode, digits);
		if (v == value) {
			return this;
		}
		return new Distance(v, units);
	}


	/**
	 * Tell if this object, represents a true or false value. In general, true
	 * is returned for any object that is not equivalent to nil, is a number
	 * greater than zero, or is the boolean true value.
	 */
	public boolean bool() {
		return value.bool();
	}


	/**
	 * Get the size of this object. The value is the number of contained objects,
	 * not the number of bytes consumed by the object. The size can be any value
	 * greater than or equal to zero.
	 */
	public int size() {
		return 1;
	}


	/**
	 * Returns a hash code value for the object. All Axil values must provide a
	 * meaningful implementation of this function to enable themselves to be
	 * placed in a map or set.
	 */
	protected int hash() {
		return value.hashCode();
	}


	/**
	 * Tell if this object is equivalent to the given object. The object given
	 * is never null. The object given may be of any type of value object.  If
	 * the given object is not a suitable type for comparison, a
	 * ClassCastException may be thrown.
	 */
	public boolean equalTo(AxilObject object) {
		if (! (object instanceof Distance)) {
			return false;
		}
		Distance other = (Distance)object;
		if (units == other.units) {
			return value.equals(other.value);
		}
		return equalTo(other.as(units));
	}


	/**
	 * Compares this object with the specified object for order. If the given
	 * object is not a suitable type for comparison, a ClassCastException may
	 * be thrown.
	 *
	 * @param object
	 * 	The object to compare against. The given object cannot be null but may
	 * 	be any Axil object.
	 *
	 * @return
	 * 	Returns a negative integer, zero, or a positive integer as this object
	 * 	is less than, equal to, or greater than the specified object.
	 */
	public int comparedTo(AxilObject object) {
		Distance other = (Distance)object;
		if (units != other.units()) {
			other = other.as(units);
		}
		return value.comparedTo(other.value);
	}
}
